<?php
include('config.inc.php');
require_once('simplexml44/IsterXmlSimpleXMLImpl.php');

function db_connect() {
	global $config;
	$link = mysql_connect($config['mysql_host'], $config['mysql_user'], $config['mysql_password'])
		or die("Could not connect: " . mysql_error() . "\n");
	mysql_select_db($config['mysql_db_name'], $link)
		or die ("Can't use ".$config['mysql_db_name']." database: " . mysql_error() . "\n");
	return $link;
}

function glog($text) {
	global $config;
	if (!isset($config['log_file']) || $config['log_file'] == '') { return; }
	$fp = fopen($config['log_file'],'a');
	if (!$fp) {
		die("Can't open ".$config['log_file']." for writing.");
	}
	fwrite($fp, date("[Y-m-d H:i:s] ", time()) . $text . "\n");
	fclose($fp);
	echo "$text\n";
}

// Parameter: String to escape
// Returns: XML-valid string data
function xmlize($string) {
	$string = str_replace("&", "&amp;", $string);
	$string = str_replace("<", "&lt;", $string);
	$string = str_replace("&amp;amp;", "&amp;", $string);
	$string = str_replace("\"", "&quot;", $string);
	return $string;
}

function magnolia_add($title, $url) {
	global $config;
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,'http://ma.gnolia.com/api/rest/1/bookmarks_add');
	curl_setopt($ch, CURLOPT_FAILONERROR, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 10);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, 'api_key='.urlencode($config['ma.gnolia.key']).'&title='.urlencode($title).'&url='.urlencode($url).'&private=0&tags=from+gobblerss');
	$result = curl_exec($ch);
	curl_close($ch);
	//$result = simplexml_load_string($result, 'simple_xml_extended');
	$impl = new IsterXmlSimpleXMLImpl();
	$result = $impl->load_string($result);
	return $result;
}

function magnolia_remove($url) {
	global $config;
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,'http://ma.gnolia.com/api/rest/1/bookmarks_find');
	curl_setopt($ch, CURLOPT_FAILONERROR, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 10);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, 'api_key='.urlencode($config['ma.gnolia.key']).'&url='.urlencode($url).'&tags=from+gobblerss');
	$result = curl_exec($ch);
	curl_close($ch);
	//$result = simplexml_load_string($result, 'simple_xml_extended');
	$impl = new IsterXmlSimpleXMLImpl();
	$result = $impl->load_string($result);

	$bookmarks = $result->response->bookmarks->children();
	if (isset($bookmarks[0])) {
		$attr = $bookmarks[0]->attributes();
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,'http://ma.gnolia.com/api/rest/1/bookmarks_delete');
		curl_setopt($ch, CURLOPT_FAILONERROR, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, 'api_key='.urlencode($config['ma.gnolia.key']).'&id='.$attr['id']);
		$result = curl_exec($ch);
		curl_close($ch);
		//$result = simplexml_load_string($result, 'simple_xml_extended');
		$impl = new IsterXmlSimpleXMLImpl();
		$result = $impl->load_string($result);
	}
	return $result;
}

$filters = null;
function filtered_article($title, $text) {
	global $filters;
	if ($filters == null) {
		$query = "SELECT id, value FROM filters";
		$result = mysql_query($query)
			or die("Could not fetch filters.");
		$filters = array();
		while ($filter = mysql_fetch_object($result)) {
			$filters[$filter->id] = $filter->value;
		}
	}
	foreach ($filters as $filter) {
		if (stripos($title, $filter) !== false) {
			return true;
		}
		if (stripos($text, $filter) !== false) {
			return true;
		}
	}
	return false;
}

function get_web_page($url, $timeout=120, $referer=null) {
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_ENCODING , "gzip,deflate");
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
	curl_setopt($ch, CURLOPT_HEADER, FALSE);
	curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

	// Fake headers; yeah, I know... not cool!
	// Still... some feeds block me if I use a custom User-Agent...
	$custom_headers = array();
	if (eregi("http://([^/]+)/", $url, $regs)) {
		$host = $regs[1];
	}
	$custom_headers[] = "Host: $host";
	$custom_headers[] = "User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.1.4) Gecko/20070531 Firefox/2.0.0.4";
	$custom_headers[] = "Accept: image/png,*/*;q=0.5";
	$custom_headers[] = "Accept-Language: en-us,en;q=0.8";
	$custom_headers[] = "Accept-Charset: UTF-8,*";
	$custom_headers[] = "Keep-Alive: 300";
	$custom_headers[] = "Connection: keep-alive";
	if ($referer != null) {
		$custom_headers[] = "Referer: $referer";
	}
	$custom_headers[] = "If-Modified-Since: Tue, 26 Apr 2005 16:57:51 GMT";
	$custom_headers[] = "If-None-Match: \"21406f-1851-d327a1c0\"";
	$custom_headers[] = "Cache-Control: max-age=0";
	curl_setopt($ch, CURLOPT_HTTPHEADER, $custom_headers);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	$result = curl_exec($ch);
	curl_close($ch);
	return $result;
}

function getLastUpdatedHTML($subscription) {
	$html = '<span id="last_updated_'.$subscription->id.'">Last updated: ';
	if (!empty($subscription->error)) {
		if (strpos($subscription->error, 'GobbleRSS: ') === 0) {
			$html .= '<img src="images/warning.gif" alt="Warning" title="header=[Warning] body=['.$subscription->error.'] cssbody=[boxOver-body] cssheader=[boxOver-header warning]" class="status" /> ';
		} else if (strpos($subscription->error, 'OK: ') === 0) {
			$html .= '<img src="images/ok.gif" alt="OK" title="header=[OK] body=['.substr($subscription->error, 4).' items found in feed.] cssbody=[boxOver-body] cssheader=[boxOver-header ok]" class="status" /> ';
		} else {
			$html .= '<img src="images/error.gif" alt="Error" title="header=[Error] body=['.$subscription->error.'] cssbody=[boxOver-body] cssheader=[boxOver-header]" class="status" /> ';
		}
	}
	if (!empty($subscription->last_updated)) {
		$html .= date('d/m/y G:i T', strtotime($subscription->last_updated));
	} else {
		$html .= 'Never';
	}
	$html .= '</span>';
	return $html;
}
?>
